package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.FinancialScoreDAO;
import cn.edu.nju.software.greencreditsystem.domain.FinancialScore;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.sql.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-7
 * 类描述：FinancialScore类DAO实现
 */
public class FinancialScoreDAOImpl implements FinancialScoreDAO
{

    @Override
    public boolean save(FinancialScore fs)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(fs);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(FinancialScore fs)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(fs);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public FinancialScore getById(int id)
    {
        FinancialScore result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (FinancialScore) s.get(FinancialScore.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialScore> getAll()
    {
        List<FinancialScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<FinancialScore> getLatest(int enterpriseID, int financialFactorID, int num)
    {
        List<FinancialScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore "
                                    + "where enterprise.id=" + enterpriseID
                                    + " and factor.id=" + financialFactorID
                                    + " order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        if(num ==0)
            return result;
        else
            return result.subList(0, Math.min(result.size(), num));
    }

    @Override
    public FinancialScore getLatest(int enterpriseID, int financialFactorID)
    {
        List<FinancialScore> list= getLatest(enterpriseID, financialFactorID, 1);
        if(list.size() > 0)
            return list.get(0);
        else return null;
    }
    
    @Override
    public double getMax(int enterpriseID, int financialFactorID)
    {
        List<FinancialScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore fs "
                                    + "where fs.enterprise.id=" + enterpriseID
                                    + " and fs.factor.id=" + financialFactorID
                                    + " order by fs.value desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        if(result == null || result.size() == 0)
            return -1;
        else
            return result.get(0).getValue();
    }

    @Override
    public double getMin(int enterpriseID, int financialFactorID)
    {
        List<FinancialScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore fs "
                                    + "where fs.enterprise.id=" + enterpriseID
                                    + " and fs.factor.id=" + financialFactorID
                                    + " order by fs.value asc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        if(result == null || result.size() == 0)
            return -1;
        else
            return result.get(0).getValue();
    }
    
    @Override
    public double getIncreaseNumber(int enterpriseID, int financialFactorID)
    {
        List<FinancialScore> list = getLatest(enterpriseID, financialFactorID, 2);
        if(list.size() > 1)
            return list.get(0).getValue() - list.get(1).getValue();
        else
            return 0;
    }
    
    @Override
    public double getIncreaseMargin(int enterpriseID, int financialFactorID)
    {
        List<FinancialScore> list = getLatest(enterpriseID, financialFactorID, 2);
        if(list.size() > 1)
            return (list.get(0).getValue() - list.get(1).getValue()) / list.get(1).getValue() * 100;
        else
            return 0;
    }
    
    @Override
    public List<FinancialScore> getHistoryIndex(int enterpriseID, int financialFactorID, Date begin, Date end)
    {
        List<FinancialScore> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore "
                                    + "where enterprise.id = " + enterpriseID
                                    + " and factor.id = " + financialFactorID
                                    + " and date >= :begin and date <= :end");
            q.setDate("begin", begin);
            q.setDate("end", end);
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public double getLatestTotalScore(int enterpriseID)
    {
        double result = 0;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from FinancialScore "
                                    + "where enterprise.id = " + enterpriseID
                                    + " and factor.name = '沃尔评分'"
                                    + " order by date desc");
            if(q.list().size() > 0)
                result += ((FinancialScore) q.list().get(0)).getValue();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
}
